﻿// HSS.Interlink.Web.FileDownloadAsync.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.html)
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2012
// ----------------------------------------------------------------------------
// File:       FileDownloadAsync.cs
// Author:     HSS\gbanta
// Created:    07/16/2011
// Modified:   07/22/2011
// ----------------------------------------------------------------------------
namespace HSS.Interlink.Web
{
	#region Using Directives
	using System;
	using System.Web;
	using System.Web.SessionState;
	#endregion

	#region FileDownloadAsync
	/// <summary>
	/// FileDownloadAsync processes download request asyncronously.
	/// </summary>
	public sealed class FileDownloadAsync : IHttpAsyncHandler, IReadOnlySessionState
	{
		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public FileDownloadAsync()
		{

		}
		#endregion

		#region IHttpAsyncHandler Members
		/// <summary>
		/// Called by the ASP.NET Runtime to begin processing the request asyncronously
		/// </summary>
		/// <param name="context">HttpContext</param>
		/// <param name="cb">The ASP.NET Callback</param>
		/// <param name="extraData">Any extra data</param>
		/// <returns><see cref="AsyncResultNoResult"/>, an instance of <see cref="IAsyncResult"/></returns>
		public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
		{
			AsyncResultNoResult async = new AsyncResultNoResult(cb, context);
			System.Threading.ThreadPool.QueueUserWorkItem(BeginProcessing, async);
			return async;
		}
		/// <summary>
		/// Called by the ASP.NET Runtime when processing has completed
		/// </summary>
		/// <param name="result">The <see cref="AsyncResultNoResult"/> created in BeginProcessRequest</param>
		public void EndProcessRequest(IAsyncResult result)
		{
			// No processing, but we have to clean up and release resources
			AsyncResultNoResult async = result as AsyncResultNoResult;
			async.EndInvoke();
		}
		#endregion

		#region IHttpHandler Members
		/// <summary>
		/// Indicates if this Handler can stay alive and be reused or if its recreated for each request
		/// </summary>
		public bool IsReusable
		{
			get { return false; }
		}
		/// <summary>
		/// This is not called by the ASP.NET Runtime when implementing
		/// <see cref="IHttpAsyncHandler"/>.
		/// </summary>
		/// <param name="context">The current <see cref="HttpContext"/> for the request being processed</param>
		public void ProcessRequest(HttpContext context)
		{
			throw new NotImplementedException();
		}
		#endregion

		#region Methods
		void BeginProcessing(object state)
		{
			AsyncResultNoResult async = state as AsyncResultNoResult;
			FileDownload downloader;
			try
			{
				HttpContext context = async.AsyncState as HttpContext;
				downloader = new FileDownload();
				downloader.ProcessRequest(context);
				async.SetAsCompleted(null, false);
			}
			catch (Exception ex)
			{
				ex.Source = Properties.Resources.Download_ExSource;
				async.SetAsCompleted(ex, false);
			}
			finally
			{
				downloader = null;
			}
		}
		#endregion
	}
	#endregion
}